#include "stm32f10x.h"                  // Device header
#include "delay.h"
#include "OLED.h"
#include "AD.h"//ADC
#include "Serial.h"//串口
#include "DHT11.h"
#include "LED.h"
#include "BEE.h"
#include "PWM.h"
#include <math.h>
#include <stdio.h>
#include <string.h>

extern uint8_t Serial_RxPacket[];
extern uint8_t RH,RL,TH,TL;
uint16_t ppm,ug;
uint8_t Flag = 0;


void OLED_ShowMain(void)
{
    OLED_ShowChineseString(1,2,0,5);//标题
    
    OLED_ShowChineseString(2,1,9,9);
    OLED_ShowChineseString(2,2,11,11);//温度
    OLED_ShowString(2,5,":");
    OLED_ShowString(2,8,"C");
    
    OLED_ShowChineseString(2,5,10,11);//湿度
    OLED_ShowString(2,13,":");
    OLED_ShowString(2,16,"%");
    
    OLED_ShowChineseString(3,1,12,13);//甲醛
    OLED_ShowString(3,5,":");
    OLED_ShowString(3,8,".");
    OLED_ShowString(3,11,"ppm");
    
    OLED_ShowChineseString(4,1,14,15);//粉尘
    OLED_ShowString(4,5,":");
    OLED_ShowString(4,9,"ug/m");
}

void Turn_voice(void)
{
    if(Flag == 0)
    {
        Flag ++;
        BEE_ON();
        LED_ON();
    }
    else if(Flag == 1)
    {
        Flag ++;
        BEE_OFF();
        LED_OFF();
    }
    Flag = Flag % 2;
}


void Data_Up(void)
{
    
    OLED_ShowNum(2,6,TH,2);//温度更新
    
    OLED_ShowNum(2,14,RH,2);//湿度更新
    
////////    OLED_ShowNum(3,6,AD_Value[1] * 0.0085,4);//甲醛计算 4000 0~10
    ppm = AD_Value[1] * 0.008;
    OLED_ShowNum(3,6,ppm/100,2);//甲醛更新
    OLED_ShowNum(3,9,ppm-ppm/100,2);
    
    ug = ((AD_Value[0]*3.3/4096)*0.17-0.1)*1000;
    OLED_ShowNum(4,6,ug,3);//粉尘更新 4000 0~500
    
    
    Serial_Printf("AT+MQTTPUB=0,\"$sys/32v2JIcbHo/Base/thing/property/post\","
              "\"{\\\"id\\\":\\\"123\\\"\\\,\\\"params\\\":{"
              "\\\"T\\\":{\\\"value\\\":%d.%d}\\\,"
              "\\\"S\\\":{\\\"value\\\":%d.%d}\\\,"
              "\\\"ppm\\\":{\\\"value\\\":0.0%d}\\\,"
              "\\\"ug\\\":{\\\"value\\\":%d}"
              "}}\",0,0\r\n",
              TH,TL,RH,RL,ppm,ug);
    if(TH>35||RH >60 || ppm > 10 || ug >120)
    {
        Turn_voice();
    }
    else
    {
        BEE_OFF();
        LED_OFF();
    }
              
    Delay_ms(1000);
}

void AT_Init()
{
    memset(Serial_RxPacket,0,80);
    Serial_Printf("AT+RST\r\n");
    Delay_ms(1000);
    
    memset(Serial_RxPacket,0,80);
    Serial_Printf("AT+CWMODE=1\r\n");
    Delay_ms(1000);
    
    memset(Serial_RxPacket,0,80);
    Serial_Printf("AT+CWJAP=\"Meimei's iPhone\",\"mm20021217\"\r\n");
    Delay_ms(5000);
    
    memset(Serial_RxPacket,0,80);
    Serial_Printf("AT+MQTTUSERCFG=0,1,\"Base\",\"32v2JIcbHo\",\"version=2018-10-31&res=products%%2F32v2JIcbHo%%2Fdevices%%2FBase&et=2536414159&method=md5&sign=fR%%2Bcilg4vicluSPFd6Uqyg%%3D%%3D\",0,0,\"\"\r\n");
    Delay_ms(1000);
    
    memset(Serial_RxPacket, 0, 80);
    Serial_Printf("AT+MQTTCONN=0,\"mqtts.heclouds.com\",1883,1\r\n");
    Delay_ms(5000);
}

//uint8_t T = 26;
//    Serial_Printf("AT+MQTTPUB=0,\"$sys/32v2JIcbHo/Base/thing/property/post\",\"{\\\"id\\\":\\\"123\\\"\\\,\\\"params\\\":{\\\"T\\\":{\\\"value\\\":%d\\\}}}\",0,0\r\n",T);
//    Delay_ms(1000);

void All_Init(void)
{
    Serial_Init();
    AD_Init();
	OLED_Init();
    LED_Init();
    BEE_Init();
    PWM_Init();
    //系统初始化
    AT_Init();
}

void OLED_ShowStart(void)
{
    OLED_ShowChineseString(2,2,4,8);//初始化
    All_Init();
    OLED_Clear();
}

int main(void)
{


	OLED_ShowStart();
    OLED_ShowMain();
	while(1)
    {
        
        receive();
        Data_Up();
    }
}	
